/*
 * AgendaNotePanel.java
 *
 * Created on 15 mars 2008, 15:15
 */

package sheep.view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;
import javax.swing.BorderFactory;
import javax.swing.JPanel;
import org.jdesktop.animation.timing.Animator;
import org.jdesktop.animation.timing.TimingTarget;
import org.jdesktop.swingx.JXPanel;
import sheep.controller.Workspace;
import sheep.model.Calendrier;
import sheep.model.Note;
import sheep.view.Component.ActionButton;
import sheep.view.Component.RoundedBox;
import sheep.view.Component.RoundedPanel;
import sheep.view.Component.SheepScrollbarUI;
import sheep.view.graphics2d.AnimationUtil;

/**
 *
 * @author  geek
 */
public class AgendaNotePanel extends JXPanel {
  
private Workspace workSpace;
  
  /** Creates new form AgendaNotePanel */
  public AgendaNotePanel(Workspace workSpace) {
    initComponents();
    this.workSpace = workSpace;

  }
  
  /** This method is called from within the constructor to
   * initialize the form.
   * WARNING: Do NOT modify this code. The content of this method is
   * always regenerated by the Form Editor.
   */
  // <editor-fold defaultstate="collapsed" desc="Generated Code">
  private void initComponents() {

    MainPanel = new JXPanel();
    centerPanel = new javax.swing.JPanel();
    
    
    mainPanel = new javax.swing.JPanel();
    titlePanel = new javax.swing.JPanel();
    titleLabel = new javax.swing.JLabel();
    titleTextField = new javax.swing.JTextField();
    calendrierPanel = new javax.swing.JPanel();
    calendrierLabel = new javax.swing.JLabel();
    calendrierComboBox = new javax.swing.JComboBox();
    locationPanel = new javax.swing.JPanel();
    locationLabel = new javax.swing.JLabel();
    locationTextField = new javax.swing.JTextField();
    beginPanel = new javax.swing.JPanel();
    beginLabel = new javax.swing.JLabel();
    beginDatePanel = new javax.swing.JPanel();
    beginDatePicker = new org.jdesktop.swingx.JXDatePicker();
    beginDateHourSpinner = new javax.swing.JSpinner();
    beginDateHourLabel = new javax.swing.JLabel();
    beginDateMinSpinner = new javax.swing.JSpinner();
    beginDateMinLabel = new javax.swing.JLabel();
    endPanel = new javax.swing.JPanel();
    endDatePanel = new javax.swing.JPanel();
    endDatePicker = new org.jdesktop.swingx.JXDatePicker();
    endDateHourSpinner = new javax.swing.JSpinner();
    endDateHourLabel = new javax.swing.JLabel();
    endDateMinSpinner = new javax.swing.JSpinner();
    endDateMinLabel = new javax.swing.JLabel();
    endLabel = new javax.swing.JLabel();
    commentPanel = new javax.swing.JPanel();
    commentLabel = new javax.swing.JLabel();
    commentScrollPane = new javax.swing.JScrollPane();
    commentTextArea = new javax.swing.JTextArea();
    
    northPanel = new javax.swing.JPanel();
    
    javax.swing.JLabel mainTitleLabel = new javax.swing.JLabel();
    
    cornerQuitBttn = new javax.swing.JButton();
    southPanel = new javax.swing.JPanel();
    validBttn = new ActionButton();
    quitBttn = new ActionButton();

    setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 45, 10, 45));
    setOpaque(false);
    setLayout(new javax.swing.BoxLayout(this, javax.swing.BoxLayout.PAGE_AXIS));

    MainPanel.setBackground(new java.awt.Color(255, 255, 255));
    MainPanel.setMaximumSize(new java.awt.Dimension(2147483647, 300));
    MainPanel.setMinimumSize(new java.awt.Dimension(73, 300));
    MainPanel.setPreferredSize(new java.awt.Dimension(100, 300));
    MainPanel.setLayout(new java.awt.BorderLayout());

    
    centerPanel.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 10, 1, 10));
    centerPanel.setOpaque(false);
    centerPanel.setLayout(new java.awt.BorderLayout());

    mainPanel.setOpaque(false);
    mainPanel.setLayout(new javax.swing.BoxLayout(mainPanel, javax.swing.BoxLayout.PAGE_AXIS));

    titlePanel.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 10, 1));
    titlePanel.setOpaque(false);
    titlePanel.setLayout(new java.awt.BorderLayout());

    titleLabel.setFont(new java.awt.Font("Times New Roman", 1, 14));
    titleLabel.setPreferredSize(new java.awt.Dimension(150, 20));
    titleLabel.setText("Titre:");
    titleLabel.setForeground(new java.awt.Color(255, 255, 255));
    titlePanel.add(titleLabel, java.awt.BorderLayout.WEST);

    titleTextField.setFont(new java.awt.Font("Times New Roman", 1, 14));
    titleTextField.setText("jTextField1");
    titleTextField.setOpaque(false);
    titleTextField.setPreferredSize(new java.awt.Dimension(450, 20));
    titlePanel.add(new RoundedPanel(titleTextField), java.awt.BorderLayout.CENTER);

    mainPanel.add(titlePanel);
    
    locationPanel.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 10, 1));
    locationPanel.setOpaque(false);
    locationPanel.setLayout(new java.awt.BorderLayout());

    locationLabel.setFont(new java.awt.Font("Times New Roman", 1, 14));
    locationLabel.setText("Emplacement :");
    locationLabel.setPreferredSize(new java.awt.Dimension(150, 20));
    locationLabel.setForeground(new java.awt.Color(255, 255, 255));
    locationPanel.add(locationLabel, java.awt.BorderLayout.WEST);

    locationTextField.setFont(new java.awt.Font("Times New Roman", 1, 14));
    locationTextField.setText("jTextField1");
    locationTextField.setOpaque(false);
    locationTextField.setPreferredSize(new java.awt.Dimension(450, 20));
    locationPanel.add(new RoundedPanel(locationTextField), java.awt.BorderLayout.CENTER);

    mainPanel.add(locationPanel);

    calendrierPanel.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 10, 1));
    calendrierPanel.setOpaque(false);
    calendrierPanel.setLayout(new java.awt.BorderLayout());

    calendrierLabel.setFont(new java.awt.Font("Times New Roman", 1, 14));
    calendrierLabel.setText("Calendier :");
    calendrierLabel.setPreferredSize(new java.awt.Dimension(150, 20));
    calendrierLabel.setForeground(new java.awt.Color(255, 255, 255));
    calendrierPanel.add(calendrierLabel, java.awt.BorderLayout.WEST);

    Color gray = new Color (180,180,180);
    calendrierComboBox.setFont(new java.awt.Font("Times New Roman", 1, 14));
    calendrierComboBox.setBackground(gray);
    calendrierComboBox.setBorder(BorderFactory.createEmptyBorder());
    calendrierComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
    calendrierComboBox.setOpaque(false);
    calendrierComboBox.setPreferredSize(new java.awt.Dimension(350, 27));
    calendrierComboBox.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        String sTemp=((String)calendrierComboBox.getModel().getSelectedItem());
        for (Calendrier cal : workSpace.calendarList)
          if (cal.getName().equals(sTemp)){
            roundedBox.setColor(cal.getColor());
            workSpace.getSelectedNote().setCalendrier(cal);
            repaint();
          }
      }
    });
    JPanel temp = new JPanel(new BorderLayout());
    temp.setOpaque(false);
    temp.setPreferredSize(new java.awt.Dimension(450, 27));
    temp.add(calendrierComboBox, java.awt.BorderLayout.CENTER);
    roundedBox = new RoundedBox (60,23,Color.WHITE);
    temp.add(roundedBox, java.awt.BorderLayout.EAST);    
    
    calendrierPanel.add(new RoundedPanel(temp), java.awt.BorderLayout.CENTER);


    
    mainPanel.add(calendrierPanel);


    beginPanel.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 10, 1));
    beginPanel.setOpaque(false);
    beginPanel.setPreferredSize(new java.awt.Dimension(327, 38));
    beginPanel.setLayout(new java.awt.BorderLayout());

    beginLabel.setFont(new java.awt.Font("Times New Roman", 1, 14));
    beginLabel.setText("Date de début");
    beginLabel.setPreferredSize(new java.awt.Dimension(150, 20));
    beginLabel.setForeground(new java.awt.Color(255, 255, 255));
    beginPanel.add(beginLabel, java.awt.BorderLayout.WEST);

    beginDatePanel.setOpaque(false);
    //beginDatePanel.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 15, 0));
    beginDatePanel.setPreferredSize(new java.awt.Dimension(450, 20));
    beginDatePanel.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.CENTER, 10, 0));

    beginDatePicker.setFont(new java.awt.Font("Times New Roman", 1, 14));
    beginDatePicker.setPreferredSize(new java.awt.Dimension(134, 22));
    beginDatePanel.add(beginDatePicker);

    beginDateHourSpinner.setFont(new java.awt.Font("Times New Roman", 1, 14));
    beginDateHourSpinner.setOpaque(false);
    beginDateHourSpinner.setPreferredSize(new java.awt.Dimension(40, 20));
    beginDatePanel.add(beginDateHourSpinner);

    beginDateHourLabel.setFont(new java.awt.Font("Times New Roman", 1, 14));
    beginDateHourLabel.setText("h");
    beginDateHourLabel.setForeground(new java.awt.Color(255, 255, 255));
    beginDatePanel.add(beginDateHourLabel);

    beginDateMinSpinner.setFont(new java.awt.Font("Times New Roman", 1, 14));
    beginDateMinSpinner.setOpaque(false);
    beginDateMinSpinner.setPreferredSize(new java.awt.Dimension(40, 20));
    beginDatePanel.add(beginDateMinSpinner);

    beginDateMinLabel.setFont(new java.awt.Font("Times New Roman", 1, 14));
    beginDateMinLabel.setText("min");
    beginDateMinLabel.setForeground(new java.awt.Color(255, 255, 255));
    beginDatePanel.add(beginDateMinLabel);

    beginPanel.add(new RoundedPanel(beginDatePanel), java.awt.BorderLayout.CENTER);

    mainPanel.add(beginPanel);

    endPanel.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 10, 1));
    endPanel.setOpaque(false);
    endPanel.setPreferredSize(new java.awt.Dimension(258, 38));
    endPanel.setLayout(new java.awt.BorderLayout());

    endDatePanel.setOpaque(false);
    endDatePanel.setPreferredSize(new java.awt.Dimension(450, 20));
    endDatePanel.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.CENTER, 10, 0));

    endDatePicker.setFont(new java.awt.Font("Times New Roman", 1, 14));
    endDatePicker.setPreferredSize(new java.awt.Dimension(134, 22));
    endDatePanel.add(endDatePicker);

    endDateHourSpinner.setFont(new java.awt.Font("Times New Roman", 1, 14));
    endDateHourSpinner.setOpaque(false);
    endDateHourSpinner.setPreferredSize(new java.awt.Dimension(40, 20));
    endDatePanel.add(endDateHourSpinner);

    endDateHourLabel.setFont(new java.awt.Font("Times New Roman", 1, 14));
    endDateHourLabel.setText("h");
    endDateHourLabel.setForeground(new java.awt.Color(255, 255, 255));
    endDatePanel.add(endDateHourLabel);

    endDateMinSpinner.setFont(new java.awt.Font("Times New Roman", 1, 14));
    endDateMinSpinner.setOpaque(false);
    endDateMinSpinner.setPreferredSize(new java.awt.Dimension(40, 20));
    endDatePanel.add(endDateMinSpinner);

    endDateMinLabel.setFont(new java.awt.Font("Times New Roman", 1, 14));
    endDateMinLabel.setText("min");
    endDateMinLabel.setForeground(new java.awt.Color(255, 255, 255));
    endDatePanel.add(endDateMinLabel);

    endPanel.add(new RoundedPanel(endDatePanel), java.awt.BorderLayout.CENTER);

    endLabel.setFont(new java.awt.Font("Times New Roman", 1, 14));
    endLabel.setText("Date de fin");
    endLabel.setPreferredSize(new java.awt.Dimension(150, 20));
    endLabel.setForeground(new java.awt.Color(255, 255, 255));
    endPanel.add(endLabel, java.awt.BorderLayout.WEST);

    mainPanel.add(endPanel);

    //commentPanel.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 10, 1));
    commentPanel.setOpaque(false);
    commentPanel.setPreferredSize(new java.awt.Dimension(258, 50));
    commentPanel.setLayout(new java.awt.BorderLayout());

    commentLabel.setFont(new java.awt.Font("Times New Roman", 1, 14));
    commentLabel.setText("Commentaires :");
    commentLabel.setPreferredSize(new java.awt.Dimension(150, 20));
    commentLabel.setForeground(new java.awt.Color(255, 255, 255));
    commentPanel.add(commentLabel, java.awt.BorderLayout.WEST);

    commentScrollPane.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
    commentScrollPane.getVerticalScrollBar().setUI(new SheepScrollbarUI());
    commentScrollPane.setPreferredSize(new java.awt.Dimension(450, 40));
    commentScrollPane.setOpaque(false);
    
    commentTextArea.setColumns(20);
    commentTextArea.setRows(5);
    commentTextArea.setOpaque(false);
    commentTextArea.setPreferredSize(new java.awt.Dimension(256, 110));
    commentScrollPane.setViewportView(commentTextArea);

    commentPanel.add(new RoundedPanel(commentScrollPane), java.awt.BorderLayout.CENTER);

    mainPanel.add(commentPanel);

    centerPanel.add(mainPanel, java.awt.BorderLayout.CENTER);    
       
    

    MainPanel.add(centerPanel, java.awt.BorderLayout.CENTER);

    northPanel.setOpaque(false);
    northPanel.setLayout(new java.awt.BorderLayout());
    northPanel.setBorder(BorderFactory.createEmptyBorder(5, 10, 15, 10));
    mainTitleLabel.setFont(new java.awt.Font("Tahoma", 1, 20));
    mainTitleLabel.setForeground(new java.awt.Color(255, 255, 255));
    mainTitleLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
    mainTitleLabel.setText("Edition notes");
    northPanel.add(mainTitleLabel, java.awt.BorderLayout.CENTER);

    cornerQuitBttn.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/images/button-close.png"))); // NOI18N
    cornerQuitBttn.setToolTipText("Close editor");
    cornerQuitBttn.setBorder(null);
    cornerQuitBttn.setBorderPainted(false);
    cornerQuitBttn.setContentAreaFilled(false);
    cornerQuitBttn.setFocusPainted(false);
    cornerQuitBttn.setFocusable(false);
    cornerQuitBttn.setMargin(new java.awt.Insets(0, 0, 0, 0));
    cornerQuitBttn.setPressedIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/images/button-close-pressed.png"))); // NOI18N
    cornerQuitBttn.setRolloverIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/images/button-close-over.png"))); // NOI18N
    cornerQuitBttn.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        cornerQuitBttnActionPerformed(evt);
      }
    });
    northPanel.add(cornerQuitBttn, java.awt.BorderLayout.EAST);

    MainPanel.add(northPanel, java.awt.BorderLayout.PAGE_START);

    //southPanel.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 1, 0, 1));
    southPanel.setOpaque(false);
    southPanel.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.RIGHT, 15, 5));

    validBttn.setText("   Ok    ");
    southPanel.add(validBttn);
    validBttn.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        OKBttnActionPerformed(evt);
      }
    });
    
    quitBttn.setText("Annuler");
    quitBttn.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        quitBttnActionPerformed(evt);
      }
    });
    southPanel.add(quitBttn);

    MainPanel.add(southPanel, java.awt.BorderLayout.PAGE_END);
    MainPanel.setOpaque(true);
    MainPanel.setBackgroundPainter(new sheep.view.utils.swingx.PainterBlackCustom());
    add(new RoundedPanel(MainPanel));
  }// </editor-fold>

  private void cornerQuitBttnActionPerformed(java.awt.event.ActionEvent evt) {
    final Container parent = this.getParent();
    Animator fadeOut = AnimationUtil.createFadeOutAnimation(this);
    fadeOut.addTarget(new TimingTarget() {
      @Override
      public void begin() {}
      @Override
      public void end() {
        setVisible2(false);
        parent.repaint();
      }
      @Override
      public void timingEvent(float f) {}
      @Override
      public void repeat() {}
    });
    fadeOut.start();
}

  private void quitBttnActionPerformed(java.awt.event.ActionEvent evt) {
        final Container parent = this.getParent();
    Animator fadeOut = AnimationUtil.createFadeOutAnimation(this);
    fadeOut.addTarget(new TimingTarget() {
      @Override
      public void begin() {}
      @Override
      public void end() {
        setVisible2(false);
        parent.repaint();
      }
      @Override
      public void timingEvent(float f) {}
      @Override
      public void repeat() {}
    });
    fadeOut.start();
  }
 
    private void OKBttnActionPerformed(java.awt.event.ActionEvent evt) {
        final Container parent = this.getParent();
    Animator fadeOut = AnimationUtil.createFadeOutAnimation(this);
    fadeOut.addTarget(new TimingTarget() {
      @Override
      public void begin() {
      //calendrierComboBox.setModel(new javax.swing.DefaultComboBoxModel(tabCat));
      //calendrierComboBox.setSelectedItem(workSpace.getSelectedNote().getCalendrier().getName());
      
      workSpace.getSelectedNote().setComment(commentTextArea.getText());
      workSpace.getSelectedNote().getDate().setTime(beginDatePicker.getDate());
      workSpace.getSelectedNote().getDate().set(Calendar.HOUR_OF_DAY, (Integer)(beginDateHourSpinner.getValue()));
      workSpace.getSelectedNote().getDate().set(Calendar.MINUTE, (Integer)(beginDateMinSpinner.getValue()));
 
      workSpace.getSelectedNote().getEndDate().setTime(endDatePicker.getDate());
      workSpace.getSelectedNote().getEndDate().set(Calendar.HOUR_OF_DAY, (Integer)(endDateHourSpinner.getValue()));
      workSpace.getSelectedNote().getEndDate().set(Calendar.MINUTE, (Integer)(endDateMinSpinner.getValue()));
      
      workSpace.getSelectedNote().setTitle(titleTextField.getText());
      workSpace.getSelectedNote().setLocation(locationTextField.getText());
      
      workSpace.getSelectedNote().getNoteZone().width=0;
      
      }
      @Override
      public void end() {
        setVisible2(false);
        parent.repaint();
      }
      @Override
      public void timingEvent(float f) {}
      @Override
      public void repeat() {}
    });
    fadeOut.start();
  }
    
    void setVisible2 (boolean aFlag){
    super.setVisible(aFlag);
    setEnabled(aFlag);
    setFocusable(aFlag);
    if (aFlag){
     if (workSpace.getSelectedNote()!=null){
      String[] tabCat = new String[workSpace.getCalendarList().size()];
      int i =0;
      for (Calendrier cal : workSpace.getCalendarList()){
        tabCat[i]=cal.getName();
        i++;
      }
      calendrierComboBox.setModel(new javax.swing.DefaultComboBoxModel(tabCat));
      calendrierComboBox.setSelectedItem(workSpace.getSelectedNote().getCalendrier().getName());
      commentTextArea.setText(workSpace.getSelectedNote().getComment());
      beginDateHourSpinner.setValue(workSpace.getSelectedNote().getDate().get(Calendar.HOUR_OF_DAY));
      beginDateMinSpinner.setValue(workSpace.getSelectedNote().getDate().get(Calendar.MINUTE));
      beginDatePicker.setDate(workSpace.getSelectedNote().getDate().getTime());
      endDateHourSpinner.setValue(workSpace.getSelectedNote().getEndDate().get(Calendar.HOUR_OF_DAY));
      endDateMinSpinner.setValue(workSpace.getSelectedNote().getEndDate().get(Calendar.MINUTE));
      endDatePicker.setDate(workSpace.getSelectedNote().getEndDate().getTime());
      titleTextField.setText(workSpace.getSelectedNote().getTitle());
      locationTextField.setText(workSpace.getSelectedNote().getLocation());
     }
    }
  }


  @Override
  public void setVisible(final boolean aFlag) {
    final Container parent = this.getParent();
    
    if (!aFlag){ 
      Animator fadeOut = AnimationUtil.createFadeOutAnimation(this);
      fadeOut.addTarget(new TimingTarget() {
        @Override
        public void begin() {}
        @Override
        public void end() {
            setVisible2(false);
            parent.repaint();
            
        }
        @Override
        public void timingEvent(float f) {}
        @Override
        public void repeat() {}
      });
      fadeOut.start();
    }else{
        Animator fadeIn = AnimationUtil.createFadeInAnimation(this);
        fadeIn.addTarget(new TimingTarget() {
        @Override
        public void begin() {setVisible2(true);}
        @Override
        public void end() {
          parent.repaint();
        }
        @Override
        public void timingEvent(float f) {}
        @Override
        public void repeat() {}
      });
      fadeIn.start();   
    }
  }
  
    private javax.swing.JLabel beginDateHourLabel;
  private javax.swing.JSpinner beginDateHourSpinner;
  private javax.swing.JLabel beginDateMinLabel;
  private javax.swing.JSpinner beginDateMinSpinner;
  private javax.swing.JPanel beginDatePanel;
  private org.jdesktop.swingx.JXDatePicker beginDatePicker;
  private javax.swing.JLabel beginLabel;
  private javax.swing.JPanel beginPanel;
  private javax.swing.JComboBox calendrierComboBox;
  private javax.swing.JLabel calendrierLabel;
  private javax.swing.JPanel calendrierPanel;
  private javax.swing.JLabel commentLabel;
  private javax.swing.JPanel commentPanel;
  private javax.swing.JScrollPane commentScrollPane;
  private javax.swing.JTextArea commentTextArea;
  private javax.swing.JLabel endDateHourLabel;
  private javax.swing.JSpinner endDateHourSpinner;
  private javax.swing.JLabel endDateMinLabel;
  private javax.swing.JSpinner endDateMinSpinner;
  private javax.swing.JPanel endDatePanel;
  private org.jdesktop.swingx.JXDatePicker endDatePicker;
  private javax.swing.JLabel endLabel;
  private javax.swing.JPanel endPanel;
  private javax.swing.JLabel locationLabel;
  private javax.swing.JPanel locationPanel;
  private javax.swing.JTextField locationTextField;
  private javax.swing.JPanel mainPanel;
  private javax.swing.JLabel titleLabel;
  private javax.swing.JPanel titlePanel;
  private javax.swing.JTextField titleTextField;
  
  // Variables declaration - do not modify
  private JXPanel MainPanel;
  private javax.swing.JPanel centerPanel;
  private javax.swing.JButton cornerQuitBttn;

  private javax.swing.JPanel northPanel;
  private javax.swing.JButton quitBttn;
  private javax.swing.JPanel southPanel;

  private javax.swing.JButton validBttn;
  // End of variables declaration
  private RoundedBox roundedBox;
}



